package com.xbongbong.saas.model.impl;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.constant.TimeConstant;
import com.xbongbong.paas.domain.entity.StageLogEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.Arith;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.constant.CommonConstant;
import com.xbongbong.saas.domain.dao.OpportunityStageLogDao;
import com.xbongbong.saas.domain.entity.OpportunityStageLogEntity;
import com.xbongbong.saas.domain.entity.ext.StageLogEntityExt;
import com.xbongbong.saas.model.OpportunityStageLogModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/3/10 16:50
 * @since v1.0
 */
@Service("opportunityStageLog")
public class OpportunityStageLogModelImpl implements OpportunityStageLogModel {

    @Resource
    private OpportunityStageLogDao dao;

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return dao.getEntitysCount(param);
    }

    @Override
    public List<OpportunityStageLogEntity> findEntitys(Map<String, Object> param) {
        return dao.findEntitys(param);
    }

    @Override
    public Map<String, Double> getAverageStayTime(String corpid, Set<Long> opportunityIdIn, List<String> stageCodeIn) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("del", 0);
        if (Objects.nonNull(opportunityIdIn)){
            param.put("dataIdIn", opportunityIdIn);
        }
        if (stageCodeIn.size() == 1){
            param.put("stageCode", stageCodeIn.get(0));
        }else {
            param.put("stageCodeIn", stageCodeIn);
        }
        List<StageLogEntityExt> stageLogList = dao.getAverageStayTime(param);

        //阶段code--平均停留时长
        Map<String, Double> dayMap = new HashMap<>(stageLogList.size());
        if (stageLogList.size() == 0) {
            return dayMap;
        }
        for (StageLogEntityExt entity : stageLogList) {
            String stageCode = entity.getStageCode();
            Double averageDay = entity.getAverageDay();
            if (stageCode == null || averageDay == null) {
                continue;
            }
            //保留一位小数
            averageDay = Arith.div(averageDay, 1, 1);
            dayMap.put(stageCode, averageDay);
        }
        return dayMap;
    }

    @Override
    public OpportunityStageLogEntity getNowStageLog(Long opportunityId, String corpid, Long formId) {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("formId", formId);
        map.put("dataId",opportunityId);
        map.put("isNow",1);
        map.put("del",0);
        map.put("start",0);
        map.put("pageNum",1);
        List<OpportunityStageLogEntity> opportunityStageLogEntityList = dao.findEntitys(map);
        if (opportunityStageLogEntityList.isEmpty()){
            return null;
        }else {
            return opportunityStageLogEntityList.get(0);
        }
    }

    @Override
    public OpportunityStageLogEntity getStageLogByStageId(Long opportunityId, String corpid, Long stageId) {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("dataId", opportunityId);
        map.put("stageId", stageId);
        map.put("del", 0);
        map.put("start", 0);
        map.put("pageNum", 1);
        List<OpportunityStageLogEntity> opportunityStageLogEntityList = dao.findEntitys(map);
        if (opportunityStageLogEntityList.isEmpty()) {
            return null;
        } else {
            return opportunityStageLogEntityList.get(0);
        }
    }

    @Override
    public String getAverageStayTime(String corpid, Long opportunityId, Long formId) throws XbbException {
        OpportunityStageLogEntity nowLog = getNowStageLog(opportunityId,corpid, formId);
        if (nowLog == null){
            return null;
        }
        Long intoTime = nowLog.getIntoTime();
        intoTime = Objects.isNull(intoTime) ? 0L : intoTime;
        Long historyStayTime = nowLog.getHistoryStayTime();
        historyStayTime = Objects.isNull(historyStayTime) ? 0L : historyStayTime;

        double stayTimeInt = (double) (historyStayTime + DateTimeUtil.getInt() - intoTime);
        Double stayTimeDay = Arith.div(stayTimeInt,TimeConstant.SECONDS_PER_DAY,1);
        return stayTimeDay + I18nMessageUtil.getMessage(CommonConstant.DAY);
    }

    @Override
    public void save(OpportunityStageLogEntity opportunityStageLogEntity) {
        if (opportunityStageLogEntity.getId() == null){
            dao.insert(opportunityStageLogEntity);
        }else {
            long now = DateTimeUtil.getInt();
            opportunityStageLogEntity.setUpdateTime(now);
            dao.update(opportunityStageLogEntity);
        }
    }

    @Override
    public List<OpportunityStageLogEntity> insertBatch(List<OpportunityStageLogEntity> opportunityStageLogList) {
         dao.insertBatch(opportunityStageLogList);
         return opportunityStageLogList;
    }

    @Override
    public Integer deleteByStageProcessIdAndDataIdIn(List<Long> dataIdIn, String corpid, Long formId, Long stageProcessId, Long stageId) {
        return dao.deleteByStageProcessIdAndDataIdIn(dataIdIn, corpid, formId, stageProcessId, stageId);
    }

    @Override
    public List<Long> getLastStageLogIdsByIntoTime(Map<String, Object> param) {
        return  dao.getLastStageLogIdsByIntoTime(param);
    }

    @Override
    public List<OpportunityStageLogEntity> updateBatch(List<OpportunityStageLogEntity> opportunityStageLogList, String corpid) {
        dao.updateBatch(opportunityStageLogList, corpid);
        return opportunityStageLogList;
    }

    @Override
    public List<OpportunityStageLogEntity> updateBatch4Script(List<OpportunityStageLogEntity> opportunityStageLogList) {
        dao.updateBatch4Script(opportunityStageLogList);
        return opportunityStageLogList;
    }

    @Override
    public OpportunityStageLogEntity getByKey(Long id, String corpid) {
        return dao.getByKey(id, corpid);
    }

    @Override
    public List<OpportunityStageLogEntity> findStageLogWithoutStage() {
        return dao.findStageLogWithoutStage();
    }

    @Override
    public Integer deleteBatch(List<StageLogEntity> stageLogEntityList, String corpid) {
        return dao.deleteBatch(stageLogEntityList, corpid);
    }

    @Override
    public boolean existOpportunityByStageProcessId(String corpid, Long formId, Long stageProcessId) {
        OpportunityStageLogEntity stageLog = dao.existOpportunityByStageProcessId(corpid, formId, stageProcessId);
        return Objects.nonNull(stageLog);
    }

    @Override
    public OpportunityStageLogEntity getIsNowStage(String corpid, Long formId, Long dataId) {
        return dao.getIsNowStage(corpid, formId, dataId);
    }

    @Override
    public Long getAvgStayTime(String corpid, Long stageProcessId, Long stageId) {
        return dao.getAvgStayTime(corpid, stageProcessId, stageId);
    }

    @Override
    public List<Long> findUsedStageProcessId(String corpid, Long formId, List<Long> stageProcessIdIn) {
        return dao.findUsedStageProcessId(corpid, formId, stageProcessIdIn);
    }
}
